import logging
import os
import re
from logging import handlers

os.makedirs("./logs", exist_ok=True)


def _logging(**kwargs):
    level = kwargs.pop('level', logging.DEBUG)
    filename = kwargs.pop('filename', 'default.log')
    datefmt = kwargs.pop('datefmt', '%Y-%m-%d %H:%M:%S')
    format = kwargs.pop('format', '[%(asctime)s,%(msecs)d][%(module)s][%(levelname)s] %(lineno)d - %(message)s')
    log = logging.getLogger(filename)
    format_str = logging.Formatter(format, datefmt)

    th = handlers.TimedRotatingFileHandler(filename=filename, when='MIDNIGHT', backupCount=5, encoding="utf-8")
    th.suffix = "%Y%m%d.log"
    th.extMatch = re.compile(r"^\d{4}\d{2}\d{2}(\.\w+)?$", re.ASCII)
    th.setFormatter(format_str)
    th.setLevel(level)

    log.addHandler(th)
    log.setLevel(level)
    return log


if __name__ == '__main__':
    logger = _logging(filename="./logs/address.log")
    logger.info("hello world")